#!/usr/bin/env Rscript

### ####################
### Package Dependencies
### ####################

library("ggplot2")
library("plyr")
library("foreach")
library("scales")

## #########
## Set Paths
## #########

p2root <- "../"
p2data <- paste(p2root, "data/CSV/mc/", sep = "")
p2figs <- paste(p2root, "doc/figs/", sep = "")

## ###############
## Combine Results
## ###############

dfOutF <- read.csv(paste(p2data, "fastest.csv", sep = ""))
dfOutFL <- read.csv(paste(p2data, "fastestLOW.csv", sep = ""))
dfOutFMP <- read.csv(paste(p2data, "fastestMP.csv", sep = ""))
dfOutO <- read.csv(paste(p2data, "other.csv", sep = ""))

dfOut <- rbind(dfOutF,
               dfOutFL,
               dfOutFMP,
               dfOutO
               )

head(dfOut)

dfOut$lab <- paste("N:",
                   format(dfOut$N, trim = TRUE, big.mark = ","),
                   "    J:",
                   format(dfOut$J, trim = TRUE, big.mark = ","),
                   sep = ""
                   )

## ###########
## Performance
## ###########

theme_set(theme_bw())

head(dfOut)

dfOut2 <- ddply(dfOut,
                .variables = c("type", "N", "J", "group"),
                function (X) median(X$times)
                )

head(dfOut2)

vX <- c(500, 1000, 2500, 5000, 7500, 10000)

vX2 <- c(500, 1000, 1500, 2000, 2500, 5000)


cols1 <- scales::hue_pal(l = 55)(6)
shapes1 <- shape_pal(solid = TRUE)(6)

cols1[c(5, 6)] <- cols1[c(6, 5)]


shapes1 <- c(1, 2, 5, 16, 7, 8)

pdf(file = paste(p2figs, "scaling_base.pdf", sep = ""),
    width = 10,
    height = 5
    )

par(mfrow = c(1, 2))

dfT1 <- subset(dfOut2, group == "J" & type %in% c("fastest", "wnominate"))
dfT1a <- subset(dfT1, type == "wnominate")
plot(dfT1a$N,
     dfT1a$V1 / 60 / 60,
     type = "b",
     col = cols1[6],
     pch = shapes1[6],
     ylab = "Hours",
     xlab = "Legislators",
     main = "Number of Bills: 1,000",
     xaxt = "n"
     )
axis(1,
     at = vX,
     labels = format(vX, big.mark = ","),
     cex.axis = .8
     )
dfT1a <- subset(dfT1, type == "fastest")
lines(dfT1a$N,
      dfT1a$V1 / 60 / 60,
      type = "b",
      col = cols1[1],
      pch = shapes1[1]
      )
text(x = c(10000, 5000),
     y = c(.3, 1),
     labels = c("EM (high-precision)", "W-NOMINATE"),
     pos = c(2, 2),
     cex = 1.1
     )
dfT2 <- subset(dfOut2, group == "N" & type %in% c("fastest", "wnominate"))
dfT2a <- subset(dfT2, type == "wnominate")
plot(dfT2a$J,
     dfT2a$V1 / 60,
     type = "b",
     col = cols1[6],
     pch = shapes1[6],
     ylab = "Minutes",
     xlab = "Bills",
     main = "Number of Legislators: 500",
     xaxt = "n"
     )
axis(1,
     at = vX2,
     labels = format(vX2, big.mark = ","),
     cex.axis = .8
     )
dfT2a <- subset(dfT2, type == "fastest")
lines(dfT2a$J,
      dfT2a$V1 / 60,
      type = "b",
      col = cols1[1],
      pch = shapes1[1]
      )
text(x = c(2500, 2500),
     y = c(1, 7),
     labels = c("EM (high-precision)", "W-NOMINATE"),
     pos = c(4, 2),
     cex = 1.1
     )

dev.off()

################################################################################
